/*
========================================================================
 Name        : ExportContactsSelectDirContainerView.cpp
 Author      : etouch.cc
 Copyright   : etouch.cc,All Right Reserved!
 Description : 
========================================================================
*/
 
#include <aknviewappui.h>
#include <eikmenub.h>
#include <avkon.hrh>
#include <barsread.h>
#include <stringloader.h>
#include <aknlists.h>
#include <eikenv.h>
#include <akniconarray.h>
#include <eikclbd.h>
#include <akncontext.h>
#include <akntitle.h>
#include <eikbtgpc.h>
#include <aknquerydialog.h> 
#include <ExportContacts.rsg>

#include "ExportContacts.hrh"
#include "ExportContactsSelectDirContainerView.h"
#include "ExportContactsListBox.hrh"
#include "ExportContactsSelectDirContainer.hrh"
#include "ExportContactsSelectDirContainer.h"
#include "ContactEngine.h" 
#include "ExportContactsAppUi.h"

CExportContactsSelectDirContainerView::CExportContactsSelectDirContainerView()
{	 
	iExportContactsSelectDirContainer = NULL;	
}

 
CExportContactsSelectDirContainerView::~CExportContactsSelectDirContainerView()
{	 
	delete iExportContactsSelectDirContainer;
	iExportContactsSelectDirContainer = NULL;	 
}
 
CExportContactsSelectDirContainerView* CExportContactsSelectDirContainerView::NewL()
{
	CExportContactsSelectDirContainerView* self = CExportContactsSelectDirContainerView::NewLC();
	CleanupStack::Pop( self );
	return self;
}
 
CExportContactsSelectDirContainerView* CExportContactsSelectDirContainerView::NewLC()
{
	CExportContactsSelectDirContainerView* self = new ( ELeave ) CExportContactsSelectDirContainerView();
	CleanupStack::PushL( self );
	self->ConstructL();
	return self;
}
  
void CExportContactsSelectDirContainerView::ConstructL()
{ 
	BaseConstructL( R_EXPORT_CONTACTS_SELECT_DIR_CONTAINER_EXPORT_CONTACTS_SELECT_DIR_CONTAINER_VIEW );
}
 
TUid CExportContactsSelectDirContainerView::Id() const
{
	return TUid::Uid( EExportContactsSelectDirContainerViewId );
}

 
void CExportContactsSelectDirContainerView::HandleCommandL( TInt aCommand )
{	 	 
	CExportContactsAppUi* appUI = (CExportContactsAppUi*)AppUi();
	
	switch ( aCommand )
	{	 
		case EExportContactsSelectDirContainerViewOpen_MenuItemCommand:
		{
			if(iExportContactsSelectDirContainer)
			{
				iExportContactsSelectDirContainer->EnterDir();
			}
		}
		break;
		case EExportContactsSelectDirContainerViewExportToDir_MenuItemCommand:
		{		
			if(iContactEngine)
			{				
				appUI->ActiveView(EExportContactsExportingContainerViewId);
				TDesC&  pExportFile = iExportContactsSelectDirContainer->GetExportFileFullName();
				if(pExportFile.Length()>0)
				{ 
					appUI->SetExportFile(pExportFile);
				}				
			}			
		}
		break;
		case EExportContactsSelectDirContainerViewMakeNewDir_MenuItemCommand:
		{		
			HBufC* pNewDir = HBufC::NewL(100);
			TPtr   ptr(pNewDir->Des());
			RunMakeNewDirQuery(ptr,EFalse);
			if(iExportContactsSelectDirContainer)
			{
				iExportContactsSelectDirContainer->MakeDir(ptr);
			}
			delete pNewDir;
		}
		break;
		case EAknSoftkeyBack:
		{ 	
			if(iExportContactsSelectDirContainer)
			{
				TBool result = iExportContactsSelectDirContainer->BackToSuperiorDir();
			    if(!result)
				{
					appUI->ActiveView(EExportContactsListBoxViewId);
				}			
			}			
		}
		break;
		default:
		break;
	}			 
}
TInt CExportContactsSelectDirContainerView::RunMakeNewDirQuery( TDes& aData, 
															    TBool aUseDefaults,
															     const TDesC* aOverridePrompt  )
{
	if ( aUseDefaults )
	{
//		HBufC* text = StringLoader::LoadLC( R_AGILE_BLOCKER_TEST_CONTAINER_EDIT1 );
//		aData.Copy( *text );
//		CleanupStack::PopAndDestroy( text );
	}
				
	CAknTextQueryDialog* queryDialog = CAknTextQueryDialog::NewL( aData );	
	
	if ( aOverridePrompt != NULL )
	{
		CleanupStack::PushL( queryDialog );
		queryDialog->SetPromptL( *aOverridePrompt );
		CleanupStack::Pop(); 
	}	
	return queryDialog->ExecuteLD( R_EXPORT_CONTACTS_SELECT_DIR_CONTAINER_MAKE_NEW_DIR_QUERY );
} 




void CExportContactsSelectDirContainerView::DoActivateL( 
														const TVwsViewId& aPrevViewId,
														TUid aCustomMessageId,
														const TDesC8& aCustomMessage )
{	 
	SetupStatusPaneL();				
	if ( iExportContactsSelectDirContainer == NULL )
	{
		iExportContactsSelectDirContainer = CreateContainerL();
		iExportContactsSelectDirContainer->SetMopParent( this );
		AppUi()->AddToStackL( *this, iExportContactsSelectDirContainer );
	} 
}
 
void CExportContactsSelectDirContainerView::DoDeactivate()
{	 
	CleanupStatusPane();
	
	if ( iExportContactsSelectDirContainer != NULL )
	{
		AppUi()->RemoveFromViewStack( *this, iExportContactsSelectDirContainer );
		delete iExportContactsSelectDirContainer;
		iExportContactsSelectDirContainer = NULL;
	}
}

void CExportContactsSelectDirContainerView::SetContactEngine(ContactEngine* aContactEngine)
{
	iContactEngine = aContactEngine;
}

void CExportContactsSelectDirContainerView::HandleStatusPaneSizeChange()
{
	CAknView::HandleStatusPaneSizeChange();	 
	TVwsViewId view;
	AppUi()->GetActiveViewId( view );
	if ( view.iViewUid == Id() )
	{
		TInt result;
		TRAP( result, SetupStatusPaneL() );
	}
}

void CExportContactsSelectDirContainerView::DynInitMenuPaneL( TInt aResourceId, CEikMenuPane* aMenuPane )
{
	if(aResourceId == R_EXPORT_CONTACTS_SELECT_DIR_CONTAINER_MENU_PANE1_MENU_PANE)
    {
		aMenuPane->SetItemDimmed(EExportContactsSelectDirContainerViewOpen_MenuItemCommand,ETrue);			 
	}
}

void CExportContactsSelectDirContainerView::SetupStatusPaneL()
{
	// reset the context pane
	TUid contextPaneUid = TUid::Uid( EEikStatusPaneUidContext );
	CEikStatusPaneBase::TPaneCapabilities subPaneContext = 
		StatusPane()->PaneCapabilities( contextPaneUid );
	if ( subPaneContext.IsPresent() && subPaneContext.IsAppOwned() )
		{
		CAknContextPane* context = static_cast< CAknContextPane* > ( 
			StatusPane()->ControlL( contextPaneUid ) );
		context->SetPictureToDefaultL();
		}
	
	// setup the title pane
	TUid titlePaneUid = TUid::Uid( EEikStatusPaneUidTitle );
	CEikStatusPaneBase::TPaneCapabilities subPaneTitle = 
		StatusPane()->PaneCapabilities( titlePaneUid );
	if ( subPaneTitle.IsPresent() && subPaneTitle.IsAppOwned() )
		{
		CAknTitlePane* title = static_cast< CAknTitlePane* >( 
			StatusPane()->ControlL( titlePaneUid ) );
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_EXPORT_CONTACTS_SELECT_DIR_CONTAINER_TITLE_RESOURCE );
		title->SetFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
				
}

void CExportContactsSelectDirContainerView::CleanupStatusPane()
{
}

CExportContactsSelectDirContainer* CExportContactsSelectDirContainerView::CreateContainerL()
{
	return CExportContactsSelectDirContainer::NewL(ClientRect(), NULL, this );
}

